<template>
  <el-dialog title="课程定位" center :visible.sync="open" width="800px" append-to-body>
    <el-form :model="queryParams" ref="queryForm" :inline="true" >
      <el-form-item label="学院" prop="collegeOrgId">
        <el-select
          v-model="queryParams.collegeOrgId"
          placeholder="请选择学院"
          @change="handleCollegeChange">
          <el-option v-for="item in collegeList" :key="item.orgName" :label="item.orgName" :value="item.orgId">
          </el-option>
        </el-select>
      </el-form-item>   
      <el-form-item label="实验室" prop="labId">
        <el-select
          v-model="queryParams.labId"
          placeholder="请选择实验室">
          <el-option v-for="item in labList" :key="item.orgName" :label="item.orgName"
            :value="item.orgId">
          </el-option>
        </el-select>
      </el-form-item>  
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" />
      <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="学院" align="center" width="150px" show-overflow-tooltip :formatter="collegeOrgIdFormat" />
          <el-table-column label="实验室" align="center" width="150px" show-overflow-tooltip :formatter="labIdFormat" />
          <el-table-column label="专业" align="center" width="150px" show-overflow-tooltip :formatter="majorOptFormat"/>
          <el-table-column label="课程编码" align="center" prop="courseNo" />
          <el-table-column label="课程名称" align="center" width="100px" show-overflow-tooltip prop="courseName" />
          <el-table-column label="实验学时" align="center" prop="experimentalHours" />
          <el-table-column label="人数" align="center" prop="personNum" />
          <el-table-column label="星期" align="center" :formatter="weekDayOptFormatter" />
          <el-table-column label="日期" align="center" width="100px" prop="courseDate" />
          <el-table-column label="节次" align="center" :formatter="sectionFormatter" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOrgByFunctionOpt, listSecondOrgLab, listOrgnization } from "@/api/labdata/orgnization";
import { listCourse } from "@/api/course/course";

export default {
  name: "Course",
  open: false,
  data() {
    return {
      invokeType: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程管理表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 课程时间表ID
      timetableId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        timetableId: null,
        collegeOrgId: null
      },
      collegeList: [],
      labList: [],
      weekDayOptOptions: [],
      majorOptOptions: [],
      // 表单参数
      form: {
        courseTeacherList: [{
          teacherNo: null,
          teacherName: null
        }],
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // 学院
    this.getCollegeList()
    // 所有学院机构
    this.getOrgnizations()
    // 星期
    this.getDicts("weekDayOpt").then(response => {
        this.weekDayOptOptions = response.data;
    });
    this.getDicts("majorOpt").then(response => {
        this.majorOptOptions = response.data;
    })
  },
  methods: {
    getOrgnizations() {
      listOrgnization({}).then(response => {
        this.orgnizationList = response.rows
      });
    },
    collegeOrgIdFormat(row, column) {
      let obj = this.collegeList.find(item => item.orgId == row.collegeOrgId) 
      if (obj) {
        return obj.orgName
      }
      return ''
    },
    labIdFormat(row, column) {
      let obj = this.orgnizationList.find(item => item.orgId == row.labId) 
      if (obj) {
        return obj.orgName
      }
      return ''
    },
    weekDayOptFormatter(row, column) {
      let obj = this.weekDayOptOptions.find(item => item.dictValue == row.weekDayOpt) 
      if (obj) {
        return obj.dictLabel
      }
      return ''
    },
    sectionFormatter(row, column) {
      return row.sectionStart + '-' + row.sectionEnd + '节'
    },
    weekDayFormatter(row, index) {
      if(row.weekDayOpt == index) {
        return row.sectionStart + '-' + row.sectionEnd + '节'
      }
    },
    majorOptFormat(row, column) {
      let obj = this.majorOptOptions.find(item => item.dictValue == row.majorOpt) 
      if (obj) {
        return obj.dictLabel
      }
      return ''
    },
    getCollegeList(){
      getOrgByFunctionOpt(2).then(response => {
        this.collegeList = response.data
      });
    },
    handleCollegeChange(item) {
      this.form.labId = null
      if (item) {
        this.getLabList(item)
      }
    },
    getLabList(id) {
      listSecondOrgLab(id).then(response => {
        this.labList = response.data
      });
    },
    /** 查询课程管理列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
    },
    handleShow() {
      this.open = true
      this.getList()
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.ids.length) {
        this.msgError("请选择课程！")
        return
      }
      this.$emit('setCourse', this.ids[0])
      this.open = false;
      this.reset();

    },
  }
};
</script>


<style scoped>
  .input_w {
    width: 380px;
  }
  .line {
    width: 10px;
  }
</style>
